package cc.linker.steplink.crm.merchant.service.impl;

import cc.linker.steplink.crm.merchant.dao.acs.ExercisePcDao;
import cc.linker.steplink.crm.merchant.service.acs.ExercisePcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ExercisePcServiceImpl implements ExercisePcService {

    @Autowired
    private ExercisePcDao exercisePcDao;

    @Override
    public void addExercise(Map<String, Object> param) {
        exercisePcDao.addExercise(param);
    }

    @Override
    public void updateExercise(Map<String, Object> param) {
        exercisePcDao.updateExercise(param);
    }

    @Override
    public void delExercise(Map<String, Object> param) {
        exercisePcDao.delExercise(param);
    }

    @Override
    public List<Map<String, Object>> list(Map<String, Object> param) {
        List<Map<String, Object>> result = exercisePcDao.list(param);
        result.forEach(x -> {
            // 触发条件名称
            String termName = "";
            String giftName = "";
            String status = "";
            if ("1".equals(x.get("term").toString()) || "2".equals(x.get("term").toString())) {
                termName += exercisePcDao.getGoodsNameByIds(x.get("termContent").toString());
                termName = StringUtils.isEmpty(termName) ? "" : "购买" + termName;
            }
            x.put("termName", termName);
            // 赠品内容名称
            if ("1".equals(x.get("giftType").toString())) {
                giftName += exercisePcDao.getGoodsNameByIds(x.get("giftContent").toString());
                x.put("giftName", giftName);
            }
            if ("2".equals(x.get("giftType").toString())) {
                x.put("giftName", "线上体检");
            }
            // 状态
            status = getExerciseStatus(x);
            x.put("status", status);
            x.remove("state");
        });
        return result;
    }

    // 计算状态
    @Override
    public String getExerciseStatus(Map<String, Object> exercise) {
        String startTime = exercise.get("startTime").toString();
        String endTime = exercise.get("endTime").toString();
        Integer state = Integer.parseInt(exercise.get("state").toString());
        String result = "进行中";
        startTime = startTime.substring(0, 19);
        endTime = endTime.substring(0, 19);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, df);
        LocalDateTime end = LocalDateTime.parse(endTime, df);
        LocalDateTime now = LocalDateTime.now();

        if (start.isAfter(now)) {
            // 未开始
            result = "未开始";
        } else if (end.isBefore(now)) {
            // 已结束
            result = "已结束";
        } else if (state == 1) {
            // 异常状态
            result = "活动异常";
        } else if (state == 2) {
            // 发放库存 判断
            int giftTotal = Integer.parseInt(exercise.get("giftTotal").toString());
            int giftSend = Integer.parseInt(exercise.get("giftSend").toString());
            if (giftTotal > giftSend) {
                // 实际查库 查看库存是否增加 触发商品是 正常商品的 才能修改状态
                if ("1".equals(exercise.get("term").toString())) {
                    // 商品库存
                    Integer termGoodsNum = getGoodsNumByID(exercise.get("termContent").toString());
                    state = termGoodsNum > 0 ? 0 : 2;
                }
            }
            if (state == 0) {
                //更新状态
                exercisePcDao.updateExerciseState(exercise.get("id").toString(), state);
                result = "进行中";
            } else {
                result = "库存不足";
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> getExercise(Map<String, Object> param) {
        Map<String, Object> result = exercisePcDao.getExercise(param);
        // 触发商品
        result.put("termList", exercisePcDao.getGiftListByIds(result.get("termContent").toString()));
        // 赠品选择列表
        List<Map<String, Object>> giftList = new ArrayList<>();
        if ("1".equals(result.get("giftType").toString())) {
            // 知识付费赠品
            giftList = exercisePcDao.getGiftListByIds(result.get("giftContent").toString());
        }
        result.put("giftList", giftList);
        return result;
    }

    @Override
    public Integer getGoodsNumByID(String goodsID) {
        return exercisePcDao.getGoodsNumByID(goodsID);
    }

    @Override
    public Integer IfRepeat(Map<String, Object> param) {
        return exercisePcDao.IfRepeat(param);
    }

    @Override
    public Integer getGoodsStateByID(String goodsID) {
        return exercisePcDao.getGoodsStateByID(goodsID);
    }

    @Override
    public Integer getRepeatTitleCount(Map<String, Object> param) {
        return exercisePcDao.getRepeatTitleCount(param);
    }

    @Override
    public Map<String, Object> getExerciseNumsById(String id) {
        return exercisePcDao.getExerciseNumsById(id);
    }
}
